package com.tiny.mirror.service.product.application.convert;

import com.api.tiny.mirror.service.product.request.CreateAppRequest;
import com.api.tiny.mirror.service.product.request.UpdateAppRequest;
import com.api.tiny.mirror.service.product.response.AppVO;
import com.api.tiny.mirror.service.product.response.ShoppingAppDetailVO;
import com.api.tiny.mirror.service.product.response.ShoppingAppVO;
import com.tiny.mirror.service.product.domain.app.App;
import com.tiny.mirror.service.product.domain.app.AppParams;
import com.tiny.mirror.service.product.infrastructure.entity.db.AppEntity;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

/**
 * @Author: liujsh
 * @Description:
 * @Date: Create in 2023/10/8
 */
@SuppressWarnings("CPD-START")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AppConvert {

  public static List<AppVO> convertToAppVOList(List<App> list) {
    if (CollectionUtils.isEmpty(list)) {
      return null;
    }
    return list.stream().map(l -> convertToAppVO(l)).collect(Collectors.toList());
  }

  public static List<AppVO> convertEntityListToAppVOList(List<AppEntity> list) {
    if (CollectionUtils.isEmpty(list)) {
      return null;
    }
    return list.stream().map(l -> convertToAppVO(l)).collect(Collectors.toList());
  }

  public static List<ShoppingAppVO> convertEntityListToDashboardAppVOList(List<AppEntity> list) {
    if (CollectionUtils.isEmpty(list)) {
      return null;
    }
    return list.stream().map(l -> convertToDashboardAppVO(l)).collect(Collectors.toList());
  }

  public static AppVO convertToAppVO(App item) {
    if (item == null) {
      return null;
    }
    AppVO result = new AppVO();
    result.setId(item.getId());
    result.setPrice(item.getPrice());
    result.setType(item.getType());
    result.setName(item.getName());
    result.setStatus(item.getStatus());
    result.setPreviewImage(item.getParams().getPreviewImage());
    result.setUserNumber(item.getParams().getUserNumber());
    result.setRolePerms(item.getParams().getRolePerms());
    result.setOrderNum(item.getOrderNum());
    result.setCreateTime(item.toEntity().getCreateTime());
    result.setUpdateTime(item.toEntity().getUpdateTime());
    return result;
  }

  public static AppVO convertToAppVO(AppEntity item) {
    if (item == null) {
      return null;
    }
    AppVO result = new AppVO();
    result.setId(item.getId());
    result.setPrice(item.getPrice());
    result.setType(item.getType());
    result.setName(item.getName());
    result.setStatus(item.getStatus());
    result.setPreviewImage(item.getParams().getPreviewImage());
    result.setUserNumber(item.getParams().getUserNumber());
    result.setRolePerms(item.getParams().getRolePerms());
    result.setOrderNum(item.getOrderNum());
    result.setCreateTime(item.getCreateTime());
    result.setUpdateTime(item.getUpdateTime());
    return result;
  }

  public static ShoppingAppVO convertToDashboardAppVO(AppEntity item) {
    if (item == null) {
      return null;
    }
    ShoppingAppVO result = new ShoppingAppVO();
    result.setId(item.getId());
    result.setPrice(item.getPrice());
    result.setType(item.getType());
    result.setName(item.getName());
    result.setPreviewImage(item.getParams().getPreviewImage());
    return result;
  }

  public static ShoppingAppDetailVO convertToDashboardAppDetailVO(AppEntity item) {
    if (item == null) {
      return null;
    }
    ShoppingAppDetailVO result = new ShoppingAppDetailVO();
    result.setId(item.getId());
    result.setPrice(item.getPrice());
    result.setType(item.getType());
    result.setName(item.getName());
    result.setPreviewImage(item.getParams().getPreviewImage());
    result.setUserNumber(item.getParams().getUserNumber());
    return result;
  }

  public static App convertToApp(CreateAppRequest request) {
    if (request == null) {
      return null;
    }
    App result = new App();
    result.setPrice(request.getPrice());
    result.setType(request.getType());
    result.setName(request.getName());
    result.setStatus(request.getStatus());
    AppParams appParams = new AppParams();
    appParams.setUserNumber(request.getUserNumber());
    appParams.setPreviewImage(request.getPreviewImage());
    result.setParams(appParams);
    result.setOrderNum(request.getOrderNum());
    return result;
  }

  public static App convertToApp(UpdateAppRequest request) {
    if (request == null) {
      return null;
    }
    App result = new App();
    result.setPrice(request.getPrice());
    result.setType(request.getType());
    result.setName(request.getName());
    AppParams appParams = new AppParams();
    appParams.setPreviewImage(request.getPreviewImage());
    result.setParams(appParams);
    result.setStatus(request.getStatus());
    result.setOrderNum(request.getOrderNum());
    return result;
  }
}
